<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<header th:replace="~{admin/header::header-fragment}"></header>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <div th:replace="~{admin/header::header-nav}"></div>
    <div th:replace="~{admin/sidebar::sidebar-fragment(${path})}"></div>

    <div class="content-wrapper">
        <!-- 面包屑导航 -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">仪表盘</h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="/admin/index">首页</a></li>
                            <li class="breadcrumb-item active">控制面板</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>

        <!-- 三个统计卡片 -->
        <div class="row">
        <div class="col-lg-4 col-6">
            <div class="small-box bg-gradient-infos">
                <div class="inner">
                    <h3 id="totalOrders">0</h3>
                    <p>订单总数</p>
                </div>
                <div class="icon">
                    <img src="/admin/dist/img/icons8-orders-60.png" class="local-icon" alt="订单">
                </div>
                <a href="/admin/orders" class="small-box-footer">查看详情
<!--                    <img src="/static/images/icons/arrow-right.png" class="arrow-icon" alt="箭头">-->
                </a>
            </div>
        </div>

        <div class="col-lg-4 col-6">
            <div class="small-box bg-gradient-success">
                <div class="inner">
                    <h3 id="totalSales">¥0</h3>
                    <p>总销售额</p>
                </div>
                <div class="icon">
                    <img src="/admin/dist/img/icons8-money-60.png" class="local-icon" alt="统计">
                </div>
                <a href="/admin/goods" class="small-box-footer">价格详情
<!--                    <img src="/static/images/icons/arrow-right.png" class="arrow-icon" alt="箭头">-->
                </a>
            </div>
        </div>

        <div class="col-lg-4 col-6">
            <div class="small-box bg-gradient-danger">
                <div class="inner">
                    <h3 id="totalUsers">0</h3>
                    <p>总用户数</p>
                </div>
                <div class="icon">
                    <img src="/admin/dist/img/icons8-people-60.png" class="local-icon" alt="用户">
                </div>
                <a href="/admin/users" class="small-box-footer">用户管理
<!--                    <img src="/static/images/icons/arrow-right.png" class="arrow-icon" alt="箭头">-->
                </a>
            </div>
        </div>
        </div>

            <!-- 热销商品 -->
            <div class="row">
                <div class="col-md-12">
                    <div class="card card-success">
                        <div class="card-header">
                            <h3 class="card-title">热销商品</h3>
                        </div>
                        <div class="card-body" id="hotProducts">
                            <div class="text-center text-muted">
                                <i class="fas fa-spinner fa-spin"></i> 加载中...
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 快捷操作 -->
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header border-0">
                            <h3 class="card-title">快捷操作</h3>
                        </div>
                        <div class="card-body">
                            <div class="row text-center">
                                <div class="col-4">
                                    <a href="/admin/goods/edit" class="btn btn-app bg-gradient-primary">
                                        <img src="/admin/dist/img/icons8-bag-50.png" class="btn-icon" alt="添加">
                                    </a>
                                    <span>添加商品</span>
                                </div>
                                <div class="col-4">
                                    <a href="/admin/orders" class="btn btn-app bg-gradient-info">
                                        <img src="/admin/dist/img/icons8-car-50.png" class="btn-icon" alt="物流">
                                    </a>
                                    <span>物流管理</span>
                                </div>
                                <div class="col-4">
                                    <a href="/admin/coupon" class="btn btn-app bg-gradient-warning">
                                        <img src="/admin/dist/img/icons8-quan-50.png" class="btn-icon" alt="优惠券">
                                    </a>
                                    <span>优惠管理</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div th:replace="~{admin/footer::footer-fragment}"></div>
</div>

<!-- 脚本部分 -->
<script th:src="@{/admin/plugins/jquery/jquery.min.js}"></script>
<script th:src="@{/admin/plugins/bootstrap/js/bootstrap.bundle.min.js}"></script>
<script th:src="@{/admin/dist/js/adminlte.min.js}"></script>

<!-- AJAX脚本 -->
<script>
    $(document).ready(function() {
        // 获取统计数据
        $.ajax({
            url: '/api/dashboard/stats',
            type: 'GET',
            success: function(data) {
                $('#totalOrders').text(data.totalOrders);
                $('#totalSales').text('¥' + data.totalSales);
                $('#totalUsers').text(data.totalUsers);
            }
        });

        // 获取热销商品
        $.ajax({
            url: '/api/dashboard/hot-products',
            type: 'GET',
            success: function(data) {
                let html = '';
                data.forEach((product, index) => {
                    html += `
                <div class="${index > 0 ? 'mt-4' : ''}">
                    <div class="d-flex justify-content-between mb-2">
                        <h5>${product.name}</h5>
                        <span>${product.sales} 件</span>
                    </div>
                    <div class="progress" style="height: 8px;">
                        <div class="progress-bar bg-success"
                             style="width: ${product.percentage}%"></div>
                    </div>
                </div>
                `;
                });
                $('#hotProducts').html(html || '<div class="text-center text-muted">暂无数据</div>');
            }
        });
    });
</script>
</body>
</html>